package com.isea.pomodoro;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Frame;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import com.sun.awt.AWTUtilities;

@SuppressWarnings("serial")
public class PomodoroGui extends JFrame {
	private final int shortTime = 300;
	private final int longTime = 600;
	private final int pomodoroTime = 1500;

	private JPanel contentPane;
	private TrayIcon trayIcon;
	private JPanel cardPanel;
	private CardLayout cardLayout;
	private JButton pomodoroBtn;
	private JButton longBtn;
	private JButton shortBtn;
	
	private Color defaulColor = new Color(175, 238, 238);
	private Color hlColor = Color.YELLOW;
	public static final Color bgColor = new Color(240, 255, 255);
	private TimePanel shortPanel;
	private TimePanel pomodoroPanel;
	private TimePanel longPanel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Throwable e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PomodoroGui frame = new PomodoroGui();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public PomodoroGui() {
		super();
		setIconImage(Icon.icon64.getImage());
		setUndecorated(true);
		setTitle("番茄时间管理");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 319);
		contentPane = new JPanel();
		contentPane.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		contentPane.setBackground(bgColor);
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		JPanel topPanel = new JPanel();
		topPanel.setBackground(bgColor);
		topPanel.setPreferredSize(new Dimension(10, 80));
		contentPane.add(topPanel, BorderLayout.NORTH);
		topPanel.setLayout(new BorderLayout(0, 0));

		shortBtn = new JButton("");
		shortBtn.setBackground(defaulColor);
		shortBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//显示短时间panel
				btnColor(e.getSource());
				cardLayout.show(cardPanel, "short");
				//开始计时
				timePanel(shortPanel);
			}
		});
		shortBtn.setToolTipText("短时间休息");
		shortBtn.setIcon(new ImageIcon(PomodoroGui.class.getResource("/com/isea/pomodoro/clock1.png")));
		shortBtn.setPreferredSize(new Dimension(80, 30));
		topPanel.add(shortBtn, BorderLayout.WEST);

		longBtn = new JButton("");
		longBtn.setBackground(defaulColor);
		longBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//显示长时间panel
				btnColor(e.getSource());
				cardLayout.show(cardPanel, "long");
				//开始计时
				timePanel(longPanel);
			}
		});
		longBtn.setToolTipText("长时间休息");
		longBtn.setIcon(new ImageIcon(PomodoroGui.class.getResource("/com/isea/pomodoro/clock2.png")));
		longBtn.setPreferredSize(new Dimension(120, 30));
		topPanel.add(longBtn, BorderLayout.EAST);

		pomodoroBtn = new JButton("番茄时间");
		pomodoroBtn.setForeground(new Color(255, 51, 0));
		pomodoroBtn.setBackground(defaulColor);
		pomodoroBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//显示番茄时间panel
				btnColor(e.getSource());
				cardLayout.show(cardPanel, "pomodoro");
				//开始计时
				timePanel(pomodoroPanel);
			}
		});
		pomodoroBtn.setFont(new Font("微软雅黑", Font.BOLD, 14));
		pomodoroBtn.setIcon(new ImageIcon(PomodoroGui.class.getResource("/com/isea/pomodoro/Pomodoro64.png")));
		topPanel.add(pomodoroBtn, BorderLayout.CENTER);

		JPanel titlePanel = new JPanel();
		titlePanel.setBorder(new EmptyBorder(0, 6, 0, 6));
		titlePanel.setBackground(bgColor);
		titlePanel.setForeground(Color.WHITE);
		titlePanel.setPreferredSize(new Dimension(10, 20));
		topPanel.add(titlePanel, BorderLayout.NORTH);
		titlePanel.setLayout(new BorderLayout(0, 0));

		MouseMoveListener mouseMoveListener = new MouseMoveListener();
		mouseMoveListener.install(this, titlePanel);

		JPanel btnPanel = new JPanel();
		btnPanel.setBackground(bgColor);
		btnPanel.setPreferredSize(new Dimension(40, 10));
		titlePanel.add(btnPanel, BorderLayout.EAST);
		btnPanel.setLayout(new BorderLayout(0, 0));

		JLabel lblX = new JLabel("X");
		lblX.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// 提示最小化或者关闭
				int result = JOptionPane.showConfirmDialog(PomodoroGui.this,
						"关闭程序选是，最小化选否", "最小化或关闭程序", JOptionPane.YES_NO_OPTION);
				if (result == JOptionPane.YES_OPTION) {
					System.exit(0);
				} else {
					// 最小化
					setVisible(false);
					minimizeToTray();
				}
			}
		});
		lblX.setForeground(new Color(0, 0, 0));
		lblX.setHorizontalAlignment(SwingConstants.CENTER);
		lblX.setPreferredSize(new Dimension(20, 18));
		btnPanel.add(lblX, BorderLayout.EAST);

		JLabel label = new JLabel("—");
		label.setBackground(bgColor);
		label.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				setVisible(false);
				minimizeToTray();
			}
		});
		label.setForeground(new Color(0, 0, 0));
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setPreferredSize(new Dimension(20, 18));
		btnPanel.add(label, BorderLayout.WEST);

		JLabel label_1 = new JLabel("番茄时间管理");
		label_1.setIcon(new ImageIcon(PomodoroGui.class.getResource("/com/isea/pomodoro/Pomodoro16.png")));
		label_1.setForeground(new Color(0, 0, 0));
		titlePanel.add(label_1, BorderLayout.WEST);
		
		cardPanel = new JPanel();
		contentPane.add(cardPanel, BorderLayout.CENTER);
		cardLayout = new CardLayout(0, 0);
		cardPanel.setLayout(cardLayout);
		
		//任务栏
		initTrayIcon();
				
		shortPanel = new TimePanel("短休",shortTime,trayIcon);
		cardPanel.add(shortPanel, "short");
		
		pomodoroPanel = new TimePanel("番茄时间",pomodoroTime,trayIcon);
		cardPanel.add(pomodoroPanel, "pomodoro");
		
		longPanel = new TimePanel("长休",longTime,trayIcon);
		cardPanel.add(longPanel, "long");
		
		JButton logBtn = new JButton("显示日志");
		logBtn.setVisible(false);
		logBtn.setBackground(defaulColor);
		contentPane.add(logBtn, BorderLayout.SOUTH);
		
		//默认显示番茄时间
		cardLayout.show(cardPanel, "pomodoro");
		btnColor(pomodoroBtn);
		//设置选择项
		shortBtn.setSelected(false);
		pomodoroBtn.setSelected(true);
		
		//圆角
		AWTUtilities.setWindowShape(this,
				new RoundRectangle2D.Double( 0.0D, 0.0D,
						getWidth(),
						getHeight(), 26.0D, 26.0D));
	}
	
	private void timePanel(TimePanel panel){
		shortPanel.reset();
		longPanel.reset();
		pomodoroPanel.reset();
		panel.start();
	}
	
	private void btnColor(Object component){
		shortBtn.setBackground(defaulColor);
		longBtn.setBackground(defaulColor);
		pomodoroBtn.setBackground(defaulColor);
		((JButton)component).setBackground(hlColor);
	}

	private void initTrayIcon() {
		PopupMenu popup = new PopupMenu();
		MenuItem showItem = new MenuItem("显示窗体");
		ActionListener listener = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(true);
				setExtendedState(Frame.NORMAL);
				SystemTray.getSystemTray().remove(trayIcon);
			}
		};
		showItem.addActionListener(listener);
		popup.add(showItem);
		
		MenuItem exitItem = new MenuItem("退出");
		exitItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				SystemTray.getSystemTray().remove(trayIcon);
				System.exit(0);
			}
		});
		popup.add(exitItem);
		
		// 根据image、提示、菜单创建TrayIcon
		this.trayIcon = new TrayIcon(Icon.icon16.getImage(), "\u756a\u8304\u65f6\u95f4\u7ba1\u7406", popup);
		// 给TrayIcon添加事件监听器
		this.trayIcon.addActionListener(listener);
	}

	public void minimizeToTray() {
		SystemTray tray = SystemTray.getSystemTray();
		try {
			tray.add(this.trayIcon);
		} catch (AWTException ex) {
			ex.printStackTrace();
		}
	}
}
